<?php

namespace Modules\Article\Entities;

use Houdunwang\Arr\Arr;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    protected $fillable = ['name','pid'];

    /**
     * 获取所有数组  树形结构的数组
     * @return array
     */
    public static function getAll(Category $category = null)
    {
        $arr = self::get()->toArray();
        //初始化组件句柄
        $arrHandler = new Arr();
        //如果不为空
        if(!is_null($category))
        {
            foreach($arr as $key => $value)
            {
                $arr[$key]['_selected'] = $value['id'] == $category['pid'];
                $arr[$key]['_disabled'] = $value['id'] == $category['id'] || $arrHandler->isChild($arr,$value['id'],$category['id'],'id');
            }
        }

        $arr = $arrHandler->tree($arr,'name','id');
        return $arr;
    }
}
